package com.xzxy.xmlg_backend.config;

import com.xzxy.xmlg_backend.Interceptor.LoginCheckInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.io.File;

@Configuration
public class WebMvcConfig implements WebMvcConfigurer {


    //拦截器对象
    @Autowired
    private LoginCheckInterceptor loginCheckInterceptor;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        //注册自定义拦截器对象
        registry.addInterceptor(loginCheckInterceptor)
                .addPathPatterns("/**")
                .excludePathPatterns("/emps/login")
                .excludePathPatterns("/captcha")
                .excludePathPatterns("/favicon.ico")
                .excludePathPatterns("/static/**")
                .excludePathPatterns("/doc.html/**")
                .excludePathPatterns("/v3/api-docs/**")
                .excludePathPatterns("/webjars/**")
                .excludePathPatterns("/avatar/**")
                .excludePathPatterns("/file/downloadStudent")
                .excludePathPatterns("/file/downloadEmp")
                .excludePathPatterns("/file/downloadClasses")
                .excludePathPatterns("/ai/generateStreamWithReasoning")
                .excludePathPatterns("/user/**");
    }
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        // 获取项目根目录
        String projectPath = System.getProperty("user.dir");
        // 头像存储的完整路径
        String avatarDir = projectPath + File.separator + "file/avatar";
        
        // 添加资源映射
        registry.addResourceHandler("/avatar/**")
                .addResourceLocations("file:" + avatarDir + File.separator);
        
        // 添加favicon静态资源映射
        registry.addResourceHandler("/favicon.ico")
                .addResourceLocations("classpath:/static/", "classpath:/public/");
        
        // 添加其他静态资源映射
        registry.addResourceHandler("/static/**")
                .addResourceLocations("classpath:/static/");
    }
}